define(function(require,exports,module) {
    // require('./plug/hammer.min');
    // require('./plug/layer/layer');
    // require('./shop');
    var myPub = require('./public');


    var myPubbaseurl = baseurl
    //评论星级
    var oEstimateStars = $('.js_estimateStars');	//评价区域
    var oEstimateStars_Lv = $('.js_estimateStars_Lv'); //评价等级信息展示
    var oNoNameCheck = $('.js_noNameCheck'); 		// 匿名选择

    var oBtnConfirm = $('.js_btnConfirm')

    //评价等级信息
    var oEstimateStars_LvInfo = ['非常差','差','一般','很好','非常好']

    var maxNum = 4;

    //评价星星设置

    oEstimateStars.each(function (i) {

        // 初始化星星状态和文字信息
        oEstimateStars.eq(i).find('li').addClass('active');
        oEstimateStars_Lv.eq(i).text(oEstimateStars_LvInfo[maxNum])

        // 添加点击事件
        oEstimateStars.eq(i).find('li').hammer().on('tap',function() {
            var index = $(this).index();
            var pIndex = oEstimateStars.index($(this).parents('.js_estimateStars'));
            var _this = $(this);

            if(_this.hasClass('active')) {
                _this.removeClass('active');

                for(var n=maxNum; n > index; n--) {
                    oEstimateStars.eq(pIndex).find('li').eq(n).removeClass('active')
                }
                if(index == 0) {
                    oEstimateStars.eq(pIndex).find('li').eq(index).addClass('active');
                    //评价信息
                    oEstimateStars_Lv.eq(pIndex).text(oEstimateStars_LvInfo[index])
                }
                else {
                    oEstimateStars_Lv.eq(pIndex).text(oEstimateStars_LvInfo[index-1])
                }
            }
            else {
                _this.addClass('active');
                if(index > 0) {
                    for(var i=0; i < index; i++) {
                        oEstimateStars.eq(pIndex).find('li').eq(i).addClass('active')
                    }
                }
                //评价信息
                oEstimateStars_Lv.eq(pIndex).text(oEstimateStars_LvInfo[index])
            }

        })
    });


    // 匿名选择
    oNoNameCheck.hammer().on('tap', function () {
        if(oNoNameCheck.find('.js_checkBox').hasClass('active')) {
            oNoNameCheck.find('.js_checkBox').removeClass('active')
        }
        else {
            oNoNameCheck.find('.js_checkBox').addClass('active')
        }
    });

    oBtnConfirm.hammer().on('tap', function () {
    	//移除监听，避免重复提交
    	oBtnConfirm.hammer().off('tap');
        submitForm();
    })

    function submitForm() {
        var discription = $("#comment").val();

        if(discription.trim() == "" || discription.trim() == null){
            myPub.TipBtnLayer("请填写评价内容！",'no',null,function () {
                myPub.LayerCloseAll();
            });
            oBtnConfirm.hammer().on('tap', function () {
            	oBtnConfirm.hammer().off('tap');
                submitForm();
            })

            return;
        }
        //去除标点符号之后，字数必须大于5
        discription=discription.replace(/[\ |\~|\`|\!|\@|\#|\$|\%|\^|\&|\*|\(|\)|\-|\_|\+|\=|\||\\|\[|\]|\{|\}|\；|\：|\《|\》|\，|\。|\？|\！|\“|\”|\;|\:|\"|\'|\,|\<|\.|\>|\/|\?]/g,"");
        if(discription.length <= 5){
            myPub.TipBtnLayer("评论内容必须多于5个字（标点符号不计算在内）",'no',null,function () {
                myPub.LayerCloseAll();
            });
            oBtnConfirm.hammer().on('tap', function () {
            	oBtnConfirm.hammer().off('tap');
                submitForm();
            })

            return;
        }
        if($('.js_noNameCheck').find('.js_checkBox').hasClass('active')) {
            $('#noName').val('true');
        }
        else {
            $('#noName').val('false');
        }
        $("#serviceStar").val($('#service_star_div').find("li.active").length);
        $("#commentstar").val($('#star_div').find("li.active").length);
        //收集图片资源
        /*var paths = "";
         $('.js_photoList').find('li').each(function(i) {
         if($('.js_view').eq(i).find('img').attr('src') != "")
         paths += $('.js_view').eq(i).find('img').attr('src')+"^";
         });
         paths = paths.substring(0,paths.length-2);
         $("#voucher").val(paths);*/
        //console.log($("#commentstar").val());
        G_JSON_POST(myPubbaseurl+"/shop/goodsComment/saveComment",$("#commentform").serialize(),function(data){

            oBtnConfirm.hammer().on('tap', function () {
            	oBtnConfirm.hammer().off('tap');
                submitForm();
            })

            if(data.status == 'success'){
                myPub.TipBtnLayer(data.message,'no',null,function () {
                    window.location.href = '/shop/pay/order_list';
                });
            }else{
                myPub.TipBtnLayer(data.message,'no',null,function () {
                    window.location.href = '/shop/pay/order_list';
                });
            }
        });
    }




});



